package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import org.tea.inventory.db.dao.AdminMapper;
import org.tea.inventory.db.domain.Admin;
import org.tea.inventory.db.domain.Admin.Column;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    public List<Admin> findAdmin(String username) {
        Admin example = new Admin();
        example.setUsername(username);
        example.setDeleted(false);
        //example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);

        return adminMapper.selectByExample(example);
    }

    private final Column[] result = new Column[]{Column.id, Column.username,Column.nickname, Column.avatar, Column.phone};
    public List<Admin> querySelective(String username, Integer page, Integer limit, String sort, String order, String nickname) {
        Admin example = new Admin();
        example.setUsername(username);
        example.setNickname(nickname);
        example.setDeleted(false);

//        LitemallAdminExample.Criteria criteria = example.createCriteria();
//        if(!StringUtils.isEmpty(username)){
//            criteria.andUsernameLike("%" + username + "%");
//        }
//        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(page, limit);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public List<Admin> querySelective() {
        Admin example = new Admin();
        example.setDeleted(false);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public int countSelective(String username, Integer page, Integer size, String sort, String order, String nickname) {
        Admin example = new Admin();
        example.setUsername(username);
        example.setNickname(nickname);
        example.setDeleted(false);

//        LitemallAdminExample.Criteria criteria = example.createCriteria();
//        if(!StringUtils.isEmpty(username)){
//            criteria.andUsernameLike("%" + username + "%");
//        }
//        criteria.andDeletedEqualTo(false);

        return (int)adminMapper.countByExample(example);
    }

    public void updateById(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    public void deleteById(Integer id) {
        Admin admin = adminMapper.selectByPrimaryKey(id);
        if(admin == null){
            return;
        }
        admin.setDeleted(true);
        adminMapper.updateByPrimaryKey(admin);
    }

    public void add(Admin admin) {
        adminMapper.insertSelective(admin);
    }

    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKeySelective(id, result);
    }

	public List<Admin> findByIdList(List ids) {
		// TODO Auto-generated method stub
		return adminMapper.findByIdList(ids,result);
	}

    public Boolean findUserName(String userName) {
        return adminMapper.findUserName(userName) > 0? true:false;
    }
}
